<template>
  <view class="micro-classroom">
    <view v-for="item in microclassList" :key="item.id" class="micro-item" @click="toMicroClassroomDetail(item)">
      <view class="item-img">
        <view class="type" :class="`type-${item.courseType % 2}`">
          {{ item.courseType % 2 ? '视频' : '音频' }}
        </view>
        <image class="image" :src="`https://shuyutk.oss-cn-qingdao.aliyuncs.com${item.img}`" mode="aspectFill" />
        <image v-if="item.vipFlag === '1'" class="item-icon" src="https://cbandu.oss-cn-qingdao.aliyuncs.com/yusj/20250603/icn_vip.png" />
        <view v-if="item.progress" class="text">
          {{ item.progress === 5 ? '已完结' : '进行中' }}
        </view>
      </view>
      <view class="item-name">
        {{ item.title }}
      </view>
    </view>
  </view>
  <ContactTeacher v-model:show-overlay="showTeacher" :type="3" />
  <LoginOverlay v-model:show="showLogin" />
</template>

<script lang="ts" setup>
import { isLogin } from '@/utils/auth';
import type { ListPageParams } from '@/api/read/type';
import { getMicroclassList } from '@/api/microClassroom';
import type { GetMicroclassListResult } from '@/api/microClassroom/type';
import { useUserStore } from '@/store';

const store = useUserStore();
const loginFlag = ref<boolean>(isLogin());
const hasSchool = computed(() => !!store.schoolId); // 是否绑定门店

const pageParams = ref<ListPageParams>({
  pageNum: 1,
  pageSize: 20,
});
const microclassList = ref<Array<GetMicroclassListResult>>([]);
const total = ref(0);
const getList = () => {
  return getMicroclassList(pageParams.value).then((res: { total: number; rows: Array<GetMicroclassListResult> }) => {
    console.log(res);
    microclassList.value = microclassList.value.concat(res.rows);
    total.value = res.total;
  });
};

onPullDownRefresh(() => {
  pageParams.value.pageNum = 1;
  microclassList.value = [];
  getList().then(() => {
    uni.stopPullDownRefresh();
  });
});

onReachBottom(() => {
  if (microclassList.value.length >= total.value)
    return;
  pageParams.value.pageNum++;
  getList();
});

const showLogin = ref<boolean>(false);
const showTeacher = ref<boolean>(false);
const toMicroClassroomDetail = (item: GetMicroclassListResult) => {
  if (item.vipFlag === '1') { // vip数据
    if (!loginFlag.value) { // 未登录
      showLogin.value = true;
      return;
    }
    if (!hasSchool.value) { // 未绑定门店
      showTeacher.value = true;
      return;
    }
    if ([0, 3].includes(store.vip.flag)) { // 非vip
      uni.$u.toast('VIP未生效！');
      return;
    }
    if (store.vip.flag === 2) { // vip过期
      uni.$u.toast('VIP已过期！');
      return;
    }
  }
  uni.navigateTo({
    url: `/pages/views/microClassroom/detail?id=${item.id}&vipFlag=${item.vipFlag}`,
  });
};

getList();

// 分享
uni.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline'],
});
onShareAppMessage(() => {
  return {
    title: store.share_txt,
    imageUrl: store.share_pic_url,
  };
});
</script>

<style lang="scss" scoped>
.micro-classroom {
  padding: 40rpx;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;

  .micro-item {
    width: 323rpx;

    &::marker {
      content: '';
      width: 0 !important;
      height: 0 !important;
    }

    .item-img {
      position: relative;
      width: 323rpx;
      height: 182rpx;
      border-radius: 16rpx;
      overflow: hidden;

      .type {
        position: absolute;
        top: 0;
        right: 0;
        padding: 0 16rpx;
        font-size: 20rpx;
        line-height: 34rpx;
        z-index: 1;
      }

      .type-1 {
        background: #FFEED1;
        color: #FF9532
      }

      .type-0 {
        background: #D4E0FC;
        color: #4977C0
      }

      .image {
        position: absolute;
        top: 0;
        left: 0;
        width: 323rpx;
        height: 182rpx;
      }

      .item-icon {
        position: absolute;
        top: 0;
        left: 0;
        width: 60rpx;
        height: 60rpx;
      }

      .text {
        position: absolute;
        right: 0;
        bottom: 0;
        padding: 0 16rpx;
        background: rgba(0,0,0,0.1);
        border-radius: 16rpx;

        font-size: 20rpx;
        color: #FFFFFF;
        line-height: 34rpx;
      }
    }

    .item-name {
      padding: 12rpx 0 36rpx 0;
      font-size: 26rpx;
      color: #000000;
      line-height: 40rpx;
    }
  }
}
</style>
